package cn.com.backstage.controller;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiGoods;
import cn.com.api.entity.ApiGoodsActType;
import cn.com.api.entity.ApiGoodsActTypeRes;
import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_goods_activity;
import cn.com.api.entity.ApiT_products;
import cn.com.api.entity.ApiType;

import cn.com.api.service.ApiGoodsActTypeService;
import cn.com.api.service.ApiGoodsService;
import cn.com.api.service.ApiT_goods_activityService;
import cn.com.api.service.ApiT_productsService;
import cn.com.api.service.StartOnLoadService;
import cn.com.api.service.ApiTypeService;

import cn.com.util.ImageUtils;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class GoodsActTypeController {

	private static Logger logger = Logger
			.getLogger(GoodsActTypeController.class);

	@Autowired
	private ApiT_goods_activityService apiT_goods_activityService;
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private ApiT_productsService productsService;
	@Autowired
	private ApiGoodsService apiGoodsService;
	@Autowired
	private ApiGoodsActTypeService apiGoodsActTypeService;
	
	
	/**
	 * 分类推荐列表
	 * @return
	 */
	@RequestMapping(value = "goodsActTypeList")
	public ModelAndView goodsActTypeList(HttpServletRequest request,
			HttpServletResponse response,ApiGoodsActType actType) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/goodsActTypeList.jsp");
		String cityid=request.getParameter("cityid");
		String enable=request.getParameter("enable");
		List<Map<String,String>> list=new ArrayList<Map<String,String>>();
		list=apiGoodsActTypeService.actTypeList(cityid,enable);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("actTypes", list);
		mv.addObject("cityid",cityid);
		mv.addObject("enable",enable);
		return mv;
	}
	
	/**
	 * 进入添加活动页面
	 */
	@RequestMapping(value = "addGoodsActTypePre")
	public ModelAndView addGoodsActTypePre(HttpServletRequest request,
			HttpServletResponse response,ApiGoodsActType actType) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/goodsActTypeAdd.jsp");
		
		String time=request.getParameter("time");
		String cityid=request.getParameter("cityid");
		String typeid=request.getParameter("typeid");
		
		List<ApiType> types=apiTypeService.getFirstTypes();//一级分类
		if(time!=null&&time!=""){
			List<ApiGoodsActTypeRes> list=new ArrayList<ApiGoodsActTypeRes>();
			list=apiGoodsActTypeService.goodsActInfo(typeid, cityid, time);
			ApiType apiType=apiTypeService.findTypeById(Integer.parseInt(typeid));
			if(apiType.getcGrade()==0){//选择的一级分类
				mv.addObject("type1",typeid);
			}else{//选择的二级分类
				List<ApiType> types2=apiTypeService.gettwoType(apiType.getcGrade());
				mv.addObject("types2",types2);
				mv.addObject("type1",apiType.getcGrade());
				mv.addObject("type2",typeid);
			}
			mv.addObject("actTypeList", list);
			mv.addObject("productSize", list.size());
		}else{
			mv.addObject("productSize","1");
		}
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("types", types);
		return mv;
	}
	
	/**
	 * 添加活动
	 * @return
	 */
	@RequestMapping(value = "addGoodsActType")
	public ModelAndView addGoodsActType(HttpServletRequest request,
			HttpServletResponse response,ApiGoodsActType actType) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/goods_list.jsp");
		String data=request.getParameter("products_list");
		String oldtime=request.getParameter("oldtime");
		String oldcity=request.getParameter("oldcity");
		String oldtype=request.getParameter("oldtype");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		actType.setCreatetime(sdf.format(new Date()));
		if(data!=null&&!"".equals(data)){
			for(String s:data.split(";")){
				String[] two=s.split(",");
				if(Integer.parseInt(two[1])>0){//更新
					actType.setProductid(two[0]);
					actType.setActid(two[1]);
					actType.setSort(Integer.parseInt(two[2]));
					apiGoodsActTypeService.update(actType);
				}else{//添加
					actType.setProductid(two[0]);
					if(oldtime!=null&&oldtime!=""){
						actType.setCreatetime(oldtime);
					}
					actType.setSort(Integer.parseInt(two[1]));
					apiGoodsActTypeService.insert(actType);
				}
			}
			}
		return mv;
	}
	
	/**
	 * 删除活动
	 * @return
	 */
	@RequestMapping(value = "delGoodsActType")
	public void delGoodsActType(HttpServletRequest request,
			HttpServletResponse response,ApiGoodsActType actType) throws Exception {
		String id=request.getParameter("id");
		if(id!=null&&id!=""){
			apiGoodsActTypeService.deleteById(id);
		}
	}
	

}
